package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 新增菜品分类or套餐分类
     *
     * @param category
     * @return
     */
    // POST http://localhost:8080/category   {"name":"下饭菜","type":"1","sort":"666"}
    @PostMapping
    public R<String> save(@RequestBody Category category) {
        log.info(category.toString());
        categoryService.save(category);
        return R.success("新增分类成功");
    }

    /**
     * 分页查询分类
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize) {
        log.info("分页查询" + page + "  :  " + pageSize);
        //构造分页构造器
        Page pageInf = new Page(page, pageSize);
        //构造条件构造器
        LambdaQueryWrapper<Category> lqw = new LambdaQueryWrapper();
        //按照sort进行排序
        lqw.orderByDesc(Category::getSort);
        //查询
        categoryService.page(pageInf, lqw);

        return R.success(pageInf);
    }

    /**
     * 根据id删除分类对象
     *
     * @param id
     * @return
     */
    //Request URL: http://localhost:8080/category?id=1522160017040515073
    //Request Method: DELETE
    @DeleteMapping
    public R<String> deleteById(Long id) { //为什么这里不用+@PathVariable？/category?id=1522160017040515073
        log.info("需要删除的对象id为：" + id);
        //优化成下面调用service自定义删除
     // categoryService.removeById(id);

        categoryService.remove(id);
        return R.success("分类删除成功！");
    }

    /**
     * 修改分类
     * @param category
     * @return
     */
    @PutMapping    //{"id":"1522585700589961217","name":"hehecai","sort":14}
    public R<String> update(@RequestBody Category category){
        //传过来的参数对象有id，直接调方法修改就行了
        categoryService.updateById(category);
        return R.success("修改成功");
    }

    /**
     * 根据条件查询分类数据
     * @param category
     * @return
     */
    @GetMapping("/list")   //访问并传过来的参数：type=1
    public R<List<Category>> list (Category category){
        //创建条件对象
        LambdaQueryWrapper<Category> lqw = new LambdaQueryWrapper();
        //设置条件      如果类型不为空，那么 where 列名 = 类型
        lqw.eq(category.getType()!= null,Category::getType,category.getType());
        //添加个排序条件 根据sort顺序升序 并根据更新时间降序
        lqw.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);

        //开查
        List<Category> list = categoryService.list(lqw);
        //返回查询集合
        return R.success(list);
    }

}
